/*
 * Copyright (C) 2009 Georgios J. Georgopoulos, All rights reserved.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.gwt.mosaic.xul.client.ui;

import java.util.List;

import org.gwt.mosaic.ui.client.layout.LayoutPanel;

import com.google.gwt.user.client.ui.Widget;

/**
 * A single tab which should be placed inside a tabbox element. The user may
 * click a tab to bring its associated page to the front.
 * 
 * @author georgopoulos.georgios(at)gmail.com
 * 
 */
public class Tab extends Container {
  private static final long serialVersionUID = 4968998170403884140L;

  private transient LayoutPanel layoutPanel = null;

  // -----------------------------------------------------------------------
  /**
   * Set to the id of a command element that is being observed by the element.
   */
  public static final String COMMAND = "command";

  public String getCommand() {
    return getString(COMMAND);
  }

  public void setCommand(String command) {
    putString(COMMAND, command);
  }

  // -----------------------------------------------------------------------
  /**
   * Indicates whether the element is disabled or not. If this element is set to
   * true the element is disabled. Disabled elements are usually drawn with
   * grayed-out text. If the element is disabled, it does not respond to user
   * actions, it cannot be focused, and the command event will not fire.
   */
  public static final String DISABLED = "disabled";

  public boolean isDisabled() {
    return getBoolean(DISABLED);
  }

  public void setDisabled(boolean disabled) {
    putBoolean(DISABLED, disabled);
  }

  // -----------------------------------------------------------------------
  /**
   * The label that will appear on the element. If this is left out, no text
   * appears.
   */
  public static final String LABEL = "label";

  public String getLabel() {
    return getString(LABEL);
  }

  public void setLabel(String label) {
    putString(LABEL, label);
  }

  // -----------------------------------------------------------------------
  public Tab() {
    super();
  }

  @Override
  protected Widget createUI() {
    layoutPanel = new LayoutPanel();

    syncUI(layoutPanel);

    return layoutPanel;
  }

  @Override
  protected void syncUI(Widget ui) {
    layoutPanel.clear();

    final List<Element> elementsWithUI = Container.searchForElementsWithUI(this);
    for (int i = 0, n = elementsWithUI.size(); i < n; ++i) {
      layoutPanel.add(elementsWithUI.get(i).getUI());
    }

    super.syncUI(ui);
  }

}
